<%@page import="java.io.*" %>
<%@ include file="/WEB-INF/template/include.jsp"%>
<openmrs:htmlInclude file="/scripts/jquery/jquery.min.js" />
<openmrs:htmlInclude file="/dwr/engine.js" ></openmrs:htmlInclude>
<openmrs:htmlInclude file="/dwr/util.js" ></openmrs:htmlInclude>
<script src="<openmrs:contextPath/>/dwr/interface/DWRDssFormEntryEvaluator.js"></script>

<%@ include file="/WEB-INF/template/header.jsp"%>



<script>
function evaluateDSS(){
    var input=document.getElementById("inputCode").value;    
    var dssname = document.getElementById("dssname").value;
    DWRDssFormEntryEvaluator.evaluateForm(input, dssname, setOutput);
}    
function setOutput(result){
    document.getElementById("resultform").value=result;
    if (result.indexOf("DSS Rule Compiled Successfully")!="-1")
        document.getElementById("saveBtn").style.visibility="visible";
}

function isRule()
{   var dssname = document.getElementById("dssname").value;
    DWRDssFormEntryEvaluator.isRule(dssname, isRuleOutput);
    
}

function isRuleOutput(result)
{
    if(result==true)
    {   var r = confirm("Rule already exists. Do you want to overwrite? Cancel to select different rule name");
        if(r==true){saveDSS();}
    
    }
    
        else saveDSS();   
}

function saveDSS()
{
    var input=document.getElementById("inputCode").value;
    var dssname = document.getElementById("dssname").value;
    DWRDssFormEntryEvaluator.saveDSSForm(input, dssname, setSaveOutput);
}

function setSaveOutput(result){
    if(result==null)
    {alert("Rule name already exists, choose a different name");
    var r = confirm("Do you want to overwrite? Cancel otherwise and use a different name.");
    if(r==true)
        {
            saveDSS();
            
        }   else {alert('Please change the rule name');}
        
        }
    else  { alert(result);}
  //  document.getElementById("alertMsg").style.visibility="visible";
}
    
function setSelectedRule() {
    var dssname = document.getElementById("select").value;
    alert(dssname)
    if(dssname==="Select a rule")
        {
            alert('Please select a rule')
        }
    else
     {   alert(dssname + "activated");
    DWRDssFormEntryEvaluator.activateDSS(dssname);}
}

function displaySelectedRule(rule)
    {   
            var dssname = rule.value;
        alert(dssname);
        document.getElementById("dssname").value=dssname.slice(0,dssname.length-4);
        DWRDssFormEntryEvaluator.displaySelectedDSS(dssname, setRule);
    }

 function setRule(result){
    document.getElementById("inputCode").value=result;
    
}   
 
</script>
<h2><spring:message code="Create New DSS Rule Set" /></h2>


<div style ="float:left;padding-right: 60px;">    
	<div style="float:left;padding-right: 135px">Title : </div>
        <input type="text" id="dssname" name="dssName"/>
        &nbsp;&nbsp;&nbsp;Select a rule:&nbsp;<select name="select" id="select" onchange="displaySelectedRule(this)">
        <option>Select a rule</option>            
        <%
        String dssrules[];
        dssrules = new File("DSS_Programs").list();
        for(String rulename : dssrules)
        {%>
        <option><%=rulename%></option>
            <%
            System.out.println(rulename);
        }
             
        %>
        </select>
        &nbsp;&nbsp;<input type="button" value="Activate" onClick="setSelectedRule()" name="activateBtn" id="activateBtn"/>
        
        <br>
        <br>
        <div style="float:left;padding-right: 50px">Enter  DSS Code :    </div>
        
<textarea name="dssCode" rows="20" cols="60" id="inputCode"/> Enter DSS Code here. </textarea><br><br>
        <div style="float:left;padding-left: 170px"></div>
        <div style="float:left;padding-left: 170px"><input type="button" value="Evaluate" onClick="evaluateDSS()"/></div>
        <div style="float:left;padding-left: 120px"><input type="button" value="Save DSS Rules." onClick="isRule()" style="visibility: hidden" id="saveBtn"/></div><br>
        <br>
        <form method="get" action="DSS_Programs\activate.dss">
        <div style="float:left;padding-left: 220px"><button type="submit">Download!</button></div>
        </form>
        <!--<div id="alertMsg" style="float:left;padding-left: 120px;color : blue;visibility: hidden">DSS Program SAVED SUCCESSFULLY</div>-->
</div>

<div style ="float:right;padding-right: 60px;">
    <h3>Result</h3>
    <textarea rows="20" cols="40" id="resultform">
    
    </textarea>
    
</div>
<%@ include file="/WEB-INF/template/footer.jsp"%>s